package com.cx.dao;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.cx.entity.Foods;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class FoodDaoImpl implements FoodDao{
    @Override
    public Foods selectFoodById(int id){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        DruidDataSource source = context.getBean(DruidDataSource.class);

        try {
            DruidPooledConnection conn = source.getConnection();
             Statement st = conn.createStatement();
             ResultSet rs = st.executeQuery("select * from dishes where id="+id);
             while(rs.next()){
                 Foods food = new Foods(rs.getInt("id"),rs.getString("name"),rs.getInt("price"),rs.getInt("stock"));
             return food;
             }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
